
import YAML from 'yaml'
import yaml from 'js-yaml'
import type { nodeType } from './index.vue'
type dataType<T> = T extends true ? string : { root: nodeType }

type res<T> = {
    data: dataType<T>,
    error: boolean
}

export let topJson = {
    "cwlVersion": "v1.0",
    "class": "CommandLineTool",
    "requirements": {
      "EnvVarRequirement": {
        "envDef": {
          "VERSION": "$(inputs.VERSION)",
          "TYPE": "$(inputs.TYPE)",
          "NCORES": "$(inputs.NCORES)",
          "JOURNAL_FILE": "$(inputs.JOURNAL_FILE.location)",
          "INPUTFILE": "$(inputs.INPUTFILE.location)"
        }
      }
    },
    "baseCommand": "echo"
  }


// jsonStr 为字符串形式的json数据
export const json2yaml = (jsonStr: string): res<true> => {
    try {
        return {
            data: yaml.dump(JSON.parse(jsonStr)),
            error: false
        }
    } catch (err) {
        return {
            data: '',
            error: true
        }
    }
}


// yamlStr 为字符串形式的yaml数据
// returnString 是否返回字符串格式的json数据
export const yaml2json = <T extends boolean>(yamlStr: string, returnString: T): res<T> => {
    try {
        return {
            data: returnString ? JSON.stringify(YAML.parse(yamlStr), null, 2) : YAML.parse(yamlStr),
            error: false
        }
    } catch (err) {
        return {
            data: '',
            error: true
        } as res<T>
    }
}

